#include <vector>
namespace lty
{
    //仿函数
    template<class T>
    struct less
    {
        bool operator()(const T& x, const T& y)
        {
            return x < y;
        }
    };
    template<class T>
    struct greater
    {
        bool operator()(const T& x, const T& y)
        {
            return x > y;
        }
    };
    template<class T, class Container = vector<T>, class Compare = less<T>>
    class priority_queue
    {
    public:
        //向上调整：尾部插入新值，找到合适位置
        void adjust_up(int child)
        {
            int parent = (child - 1) / 2;
            while (child > 0)
            {
                //传入两个要对比的值，在调用优先级队列时，传入需要建立的堆类型，然后调用仿函数，返回对应比较值
                if (Compare()(_con[parent], _con[child]))//匿名函数
                {
                    swap(_con[child], _con[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }
        //向下调整：第一个节点往下调整找到合适位置（下面全是完整堆）
        void adjust_down(int parent)
        {
            size_t child = parent * 2 + 1;
            while (child < _con.size())
            {
                if (child + 1 < _con.size()
                    && Compare()(_con[child],_con[child + 1]))
                {
                    ++child;
                }

                if (Compare()(_con[parent], _con[child]))
                {
                    swap(_con[child], _con[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }

        void push(const T& x)
        {
            _con.push_back(x);
            adjust_up(_con.size()-1);//每次尾插后都要调整一次
        }

        void pop()
        {
            swap(_con[0], _con[_con.size() - 1]);//删除最顶端的值，再向下调整，交换到尾部，实现堆排序
            _con.pop_back();//尾删
            adjust_down(0);
        }

        const T& top()
        {
            return _con[0];//堆顶
        }

        size_t size()
        {
            return _con.size();
        }

        bool empty()
        {
            return _con.empty();
        }
    private:
        Container _con;
    };

    void test1()
    {
        //默认建立大堆less(降序)
//        priority_queue<int> pq1;
        priority_queue<int,vector<int>,greater<int>> pq1;//需要建立小堆则需传入三个参数，第三个判断大小堆
        pq1.push(1);
        pq1.push(3);
        pq1.push(9);
        pq1.push(4);
        pq1.push(7);
        pq1.push(0);
        pq1.push(2);
        while(!pq1.empty())
        {
            cout << pq1.top() << " ";
            pq1.pop();
        }
        cout << endl;
    }

    class Date
    {
    public:
        Date(int year = 1900, int month = 1, int day = 1)
                : _year(year)
                , _month(month)
                , _day(day)
        {}

        bool operator<(const Date& d)const
        {
            return (_year < d._year) ||
                   (_year == d._year && _month < d._month) ||
                   (_year == d._year && _month == d._month && _day < d._day);
        }

        bool operator>(const Date& d)const
        {
            return (_year > d._year) ||
                   (_year == d._year && _month > d._month) ||
                   (_year == d._year && _month == d._month && _day > d._day);
        }

        friend ostream& operator<<(ostream& _cout, const Date& d)
        {
            _cout << d._year << "-" << d._month << "-" << d._day;
            return _cout;
        }

    private:
        int _year;
        int _month;
        int _day;
    };

    class PDateLess
    {
    public:
        bool operator()(const Date* p1, const Date* p2)
        {
            return *p1 < *p2;
        }
    };
    class PDateGreater
    {
    public:
        bool operator()(const Date* p1, const Date* p2)
        {
            return *p1 > *p2;
        }
    };

    void test2()
    {
        // 大堆，需要用户在自定义类型中提供<的重载
        priority_queue<Date, vector<Date>, greater<Date>> q1;
        q1.push(Date(2018, 10, 29));
        q1.push(Date(2018, 10, 30));
        q1.push(Date(2018, 10, 28));
        cout << q1.top() << endl;

        priority_queue<Date*, vector<Date*>, PDateGreater> q2;
        q2.push(new Date(2018, 10, 29));//传的是地址，要新建仿函数
        q2.push(new Date(2018, 10, 30));
        q2.push(new Date(2018, 10, 28));
        cout << *(q2.top()) << endl;
    }


}